/**
 * jwt 验证
 */
var _ = require("lodash");
var jwt = require('jsonwebtoken');
var passport = require('passport');
var passportJWT = require('passport-jwt');

var ExtractJwt = passportJWT.ExtractJwt;
var JwtStrategy = passportJWT.Strategy;//策略
var users = [
  {
    id: 1,
    name: 'admin',
    password: '%2yx4'
  },
  {
    id: 2,
    name: 'test',
    password: 'test'
  }
];
var jwtOptions = {};
jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
jwtOptions.secretOrKey = 'szjedu';
exports.passportInit = function(){
    var strategy = new JwtStrategy(jwtOptions, function (jwt_payload, next) {
        
            // usually this would be a database call:
        
            var user = users[_.findIndex(users, { id: jwt_payload.id })];
            console.log(user);
            if (user) {
              next(null, user);
            } else {
              next(null, false);
            }
          });
          passport.use(strategy) //使用策略

}

     